#!/usr/bin/env   python
#coding:utf8
import os
import shutil

def sync(path):
    path='.'
    op = os.path
    user_dir = op.expanduser("~")
    sync_files = [i for i in os.listdir(path) if i[0] == "_"]
    for i in sync_files:
        target_path = op.join(user_dir, ".%s" % i[1:])
        print "Sync file %s to %s..." % (i, target_path)
        if os.path.exists(target_path):
            print "Moving %s(old file) to %s" % (target_path, "%s.back" % target_path)
            c = "%s.back" % target_path 
            n = 0
            while os.path.exists(("%s%s" % (c, n)) if n else c):
                n = n + 1
            shutil.move(target_path, ("%s%s" % (c, n)) if n else c)
        if os.path.isdir(i):
            shutil.copytree(i, target_path)
        else:
            shutil.copy2(i, target_path)

    print "OK!"
    
if __name__ == "__main__":
    sync(".")

